<template>
  <div class="app-container">
    <el-card header="云端录像">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="RefreshRight" @click="getGBCloudRecords"
                     v-hasPermi="['iot:gb:cloudRecord']">刷新
          </el-button>
        </el-col>
      </el-row>
      <el-table v-loading="loading" :data="recordList" border>
        <el-table-column prop="app" label="应用名" align="center"></el-table-column>
        <el-table-column prop="stream" label="流ID" width="380" align="center"></el-table-column>
        <el-table-column label="开始时间" align="center">
          <template v-slot:default="scope">
            {{ formatTimeStamp(scope.row.startTime) }}
          </template>
        </el-table-column>
        <el-table-column label="结束时间" align="center">
          <template v-slot:default="scope">
            {{ formatTimeStamp(scope.row.endTime) }}
          </template>
        </el-table-column>
        <el-table-column label="时长" align="center">
          <template v-slot:default="scope">
            <el-tag>{{ formatTime(scope.row.timeLen) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="fileName" label="文件名称" align="center"></el-table-column>
        <el-table-column prop="mediaServerId" label="流媒体" align="center"></el-table-column>
        <el-table-column label="操作" width="100" fixed="right" align="center">
          <template v-slot:default="scope">
            <el-button size="medium" icon="VideoPlay" type="text" @click="play(scope.row)">播放</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
          v-show="total>0"
          :total="total"
          v-model:page="queryParams.page"
          v-model:limit="queryParams.count"
          @pagination="getGBCloudRecords"
      />
      <el-dialog title="播放视频" v-model="showVideoPlayer" width="700px" append-to-body>
        <easyPlayer ref="recordVideoPlayer" :videoUrl="vUrl" :height="false"  ></easyPlayer>
      </el-dialog>
    </el-card>
  </div>
</template>

<script setup name="CloudRecord">
import {getCloudRecordList, getPlayUrlPath} from "@/api/iot/cloudRecord";
import {onMounted} from "vue";
import easyPlayer from './components/easyPlayer.vue'

function formatTimeStamp(time) {
  const date = new Date(time);
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0');
  const day = String(date.getDate()).padStart(2, '0');
  const hours = String(date.getHours()).padStart(2, '0');
  const minutes = String(date.getMinutes()).padStart(2, '0');
  const seconds = String(date.getSeconds()).padStart(2, '0');
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

function formatTime(time) {
  const h = parseInt(time / 3600 / 1000)
  const minute = parseInt((time - h * 3600 * 1000) / 60 / 1000)
  let second = Math.ceil((time - h * 3600 * 1000 - minute * 60 * 1000) / 1000)
  if (second < 0) {
    second = 0;
  }
  return (h > 0 ? h + `小时` : '') + (minute > 0 ? minute + '分' : '') + (second > 0 ? second + '秒' : '')
}

const queryParams = ref({
  page: 1,
  count: 10,
})
const recordList = ref([]);
const total = ref(0);
const loading = ref(true);

async function getGBCloudRecords() {
  const res = await getCloudRecordList(queryParams.value);
  if (res.data){
    recordList.value = res.data.list;
    total.value = res.data.total;
  }
  loading.value = false;
}

/**
 * 播放
 * @param row
 */
const vUrl = ref('');
const showVideoPlayer = ref(false);
const play = async (row) => {
  const params = {
    recordId: row.id,
  }
  const res = await getPlayUrlPath(params);
  const currentProtocol = window.location.protocol;
  if (currentProtocol === 'https:') {
    vUrl.value =  res.data.httpsPath
  } else {
    vUrl.value =  res.data.httpPath
  }
  showVideoPlayer.value = true;
}

onMounted(async () => {
  await getGBCloudRecords();
})
</script>

<style scoped>

</style>